<?php
if (!function_exists('fnmatch')) {
	function fnmatch($pattern, $string) {
		for ($op = 0, $npattern = '', $n = 0, $l = strlen($pattern); $n < $l; $n++) {
			switch ($c = $pattern[$n]) {
				case '\\':
					$npattern .= '\\' . @$pattern[++$n];
					break;
				case '.': case '+': case '^': case '$': case '(': case ')': case '{': case '}': case '=': case '!': case '<': case '>': case '|':
					$npattern .= '\\' . $c;
					break;
				case '?': case '*':
					$npattern .= '.' . $c;
					break;
				case '[': case ']': default:
					$npattern .= $c;
				if ($c == '[') {
					$op++;
				} else if ($c == ']') {
					if ($op == 0) return false;
					$op--;
				}
				break;
			}
		}		
		if ($op != 0) return false;
		
		return preg_match('/' . $npattern . '/i', $string);
	}
}

$localfolder = $argv[1];
$logfile = "log_analyze.txt";
$logfile1 = "log_analyze_identical.txt";
$logfile2 = "log_analyze_special_key.txt";

$print_file_counts=true;//this can help you find new templates that have not been translated!

$hash_list = array();//The md5 sums mapped to translations

$hash_list_file = array();//The md5 sums mapped to translations
$order   = array("\r\n", "\n", "\r");
$endline ="\r";

$header = "msgid \"\"
	msgstr \"\"
	\"Project-Id-Version: AthenaOne\\n\"
	\"POT-Creation-Date: \\n\"
	\"PO-Revision-Date: \\n\"
	\"Last-Translator: \\n\"
	\"Language-Team: \\n\"
	\"MIME-Version: 1.0\\n\"
	\"Content-Type: text/plain; charset=utf-8\\n\"
	\"Content-Transfer-Encoding: 8bit\\n\"";


$file_word_count = array();// for recording what files have been translated and how may items where in each file
$supportedLanguage = array('vi_VN','en_US');
$defaultLang = 'vi_VN';


/*
 if (!file_exists('languages')) {
 mkdir('languages');	
 foreach ( $supportedLanguage as $value ) {
 mkdir("languages/$value");
 mkdir("languages/$value/common");		
 }       
 }		
 */
$rootfolder = realpath(dirname(__FILE__). '/..');

if (!file_exists($rootfolder."/$localfolder/languages/$defaultLang")) {
	mkdir($rootfolder."/$localfolder/languages/$defaultLang",0777,true);
}

/////////////load key of current file/////
$outFile = realpath(dirname(__FILE__). '/..') ."/$localfolder/languages/$defaultLang/default.po";
$flog = fopen($logfile, 'w') or die("can't open file");
$flog1 = fopen($logfile1, 'w') or die("can't open file");
$flog2 = fopen($logfile2, 'w') or die("can't open file");
fwrite($flog2, $header);  

if (file_exists($outFile)) {
	
	$fh = fopen($outFile, 'r') or die("can't open file");	
	
	$patterngetkey = '!msgid[\s]+["](.*?)["][\s]*msgstr!s';	
	$patterngetvalue = '!msgstr[\s]+["](.*?)["][\s]*(#|msgid)!s';
	if (filesize($outFile) >0) {
		$currentcontent = fread($fh, filesize($outFile));
		preg_match_all($patterngetkey, $currentcontent, $matcheskey);
		preg_match_all($patterngetvalue, $currentcontent, $matchesvalue);
		
		for ( $index = 0, $max_count = sizeof( $matcheskey[1] ); $index < $max_count; $index++ ) {
			$newmkey = str_replace($order, $endline, $matcheskey[1][$index]);
			$hash_list_dup[$newmkey][$index]= $matchesvalue[1][$index];	
		}
		
	}	
	
	fclose($fh);	
}

$count = 1;
$count1 = 1;
$count2 = 1;
foreach ( $hash_list_dup as $key=>$value ) {	
	if (count($value) > 1) {
		fwrite($flog, "#### num: [$count1] duplicated word: [$key]###\n");
		foreach ( $value as $v ) {
			fwrite($flog, "content : [$v]\n");       
		}
		fwrite($flog, "\n\n");
		$count1 += 1;
	} else {
		foreach ( $value as $v ) {			
			fwrite($flog1, "# num : [$count] word: [$key] value:[$v]\n");       
		}
		$count += 1;
		
	}
	preg_match_all('![\s]*(.*?)[_][0-9]!s', $key, $matchesspkey);
	if (count($matchesspkey[1]) > 0)
	{
		
		fwrite($flog2, "#### num: [$count2] special word: [$key]###\n");
		fwrite($flog2, "msgid \"".$key."\"\n");
		foreach ( $value as $v ) {
			fwrite($flog2, "msgstr \"".$v."\"\n\n");       
		}
			
			
		$count2 += 1;
	}
       
}

fclose($flog);
fclose($flog1);
//fwrite($flog, "(IGNORED EMPTY FILE) File name : ".$file."\n");


//$file_word_count[$file]=count($matches[1]);// how many words where in this file


function list_directory($dir) {
	$file_list = '';
	$stack[] = $dir;
	while ($stack) {
		$current_dir = array_pop($stack);
		if ($dh = opendir($current_dir)) {
			while (($file = readdir($dh)) !== false) {
				if ($file !== '.' AND $file !== '..') {
					$current_file = "{$current_dir}/{$file}";
					if (is_file($current_file)) {						
						$file_list[] = "{$current_dir}/{$file}";
					} elseif (is_dir($current_file)) {
						$stack[] = $current_file;
					}
				}
			}
		}
	}
	return $file_list;
}
?>
